import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store/index"

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/users/manage"
  },
  {
    path: "/users/manage",
    component: () => import("@/views/userManage/UserManage"),
    meta: {
      name: "人员管理"
    }
  },
  {
    path: "/users/list",
    component: () => import("@/views/userList/UserList"),
    meta: {
      name: "花名册"
    }
  },
  {
    path: "/department/manage",
    component: () => import("@/views/deptManage/DeptManage"),
    meta: {
      name: "部门管理"
    }
  },
  {
    path: "/403",
    component: {
      render: h => h("h3", {}, ['403 Forbidden'])
    }
  },
  {
    path: "*",
    component: {
      render: h => h("h3", {}, ['404 Not Found'])
    }
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to就是目标的$route, form就是当前的$route, next给你来调用的
  // haveAuth : permissionId + token
  if (!store.state.token) {
    next("/403")
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  document.title = `才气|${to.meta.name}` || "才气软件"
})


export default router
